clear
clear matrix
set mem 500m
set more off

capture log close

local control "illness_weekdays"
local varlist "use_normal ever_anycare ever_public ever_highlvl_pb ever_lowlvl_pb ever_private ever_drugshop took_malariatest took_medicine took_any_AM took_AB any_drugcost length_illness_1wkormore"

local switch_full = 1
local switch_u5kids = 0

*** Table 3, Panel A 
if `switch_full' == 1 {
	
	use $working_data/Absenteeism_final_sample.dta, clear	

	sum `varlist'
	local num_varlist: word count `varlist'
	di "Number of Outcome variables is `num_varlist'"
	mat define b = J(`num_varlist', 1, .)
	mat define se = J(`num_varlist', 1, .)
	mat define sig = J(`num_varlist', 1, .)
	mat define N = J(`num_varlist', 1, .)	
	mat define lowerci = J(`num_varlist', 1, .)	
	mat define upperci = J(`num_varlist', 1, .)	
	mat define mean_y = J(`num_varlist', 1, .)
	
	local i = 0
	foreach var of local varlist {
		  
	  local ++i
	  clogit `var' Absent_normal `control' i.month, vce(cluster normal_hfid) group(household_id) or
	  summ `var' if e(sample) == 1
	  mat mean_y[`i', 1] =  round(r(mean),.001)
	  mat b[`i', 1] = round(exp(_b[Absent_normal]),.001)
	  mat se[`i', 1] = round(_se[Absent_normal],.001)
	  local p = round(2*(normal(-abs(_b[Absent_normal]/_se[Absent_normal]))),.0001)	
	  mat sig[`i', 1] = `p'
	  mat N[`i', 1] =  e(N)
	  mat lowerci[`i', 1] = round(exp(_b[Absent_normal] - 1.96 * _se[Absent_normal]),.001)
	  mat upperci[`i', 1] = round(exp(_b[Absent_normal] + 1.96 * _se[Absent_normal]),.001)
	}
	

	keep `varlist'
	order `varlist'
	
	keep in 1
	xpose, clear varname
	replace v1 = _n
	
	svmat mean_y
	svmat b
	svmat sig	
	
	* format coef
	gen coef = string(b, "%9.3f")
	replace coef = coef + "*" if sig < 0.1 & sig >= 0.05
	replace coef = coef + "**" if sig < 0.05 & sig >= 0.01
	replace coef = coef + "***" if sig < 0.01
	
	svmat se
	gen sd = "(" + string(se, "%9.3f") + ")"
	
	keep coef* sd* b* v1 _varname mean_y* sig*
	
	svmat lowerci
	svmat upperci
	
	* format 95% confidence interval
	gen ci = "["+ string(lowerci, "%9.3f") + ", " + string(upperci, "%9.3f") + "]"

	svmat N
	order v1 _varname mean_y coef sig ci N 
	
	insobs 1, before(1)
	replace v1 = 0 in 1
	replace _varname = "use" if v1 == 0
	

	replace mean_y = mean_y[2] if v1 == 0
	replace coef = coef[2] if v1 == 0
	replace sd = sd[2] if v1 == 0
	replace sig = sig[2] if v1 == 0
	replace N = N[2] if v1 == 0
	replace b = b[2] if v1 == 0
	replace ci = ci[2] if v1 == 0
	replace lowerci = lowerci[2] if v1 == 0
	replace upperci = upperci[2] if v1 == 0

	drop if inlist(_varname, "use_normal")
	
	replace _varname = "Use the facility" if _varname == "use"
	replace _varname = "Seek facility-based care" if _varname == "ever_anycare"
	replace _varname = "Go to public facilities" if _varname == "ever_public"
	replace _varname = "Go to high level public facilities" if _varname == "ever_highlvl_pb"
	replace _varname = "Go to low level public facilities" if _varname == "ever_lowlvl_pb"
	replace _varname = "Go to private facilities" if _varname == "ever_private"
	replace _varname = "Go to retail sectors" if _varname == "ever_drugshop"
	replace _varname = "Receive malaria test" if _varname == "took_malariatest"
	replace _varname = "Take medications" if _varname == "took_medicine"
	replace _varname = "Number of medications" if _varname == "n_drug"
	replace _varname = "Take any antimalarial drugs" if _varname == "took_any_AM"
	replace _varname = "Take antibiotics" if _varname == "took_AB"
	replace _varname = "Length of illness (days)" if _varname == "length_illness"
	replace _varname = "Ln(OOP for medications)" if _varname == "ln_drugcost"
	replace _varname = "Pay OOP for medications" if _varname == "any_drugcost"
	replace _varname = "Illness lasts more than one week" if _varname == "length_illness_1wkormore"	
	
}




*** Table 3, Panel B
if `switch_u5kids' == 1 {

use $working_data/Absenteeism_final_sample.dta, clear		
	
	keep if age <= 5

	sum `varlist'
	local num_varlist: word count `varlist'
	di "Number of Outcome variables is `num_varlist'"
	mat define b = J(`num_varlist', 1, .)
	mat define se = J(`num_varlist', 1, .)
	mat define sig = J(`num_varlist', 1, .)
	mat define N = J(`num_varlist', 1, .)	
	mat define lowerci = J(`num_varlist', 1, .)	
	mat define upperci = J(`num_varlist', 1, .)	
	mat define mean_y = J(`num_varlist', 1, .)
	
	local i = 0
	foreach var of local varlist {
		  
	  local ++i
	  clogit `var' Absent_normal `control' i.month, vce(cluster normal_hfid) group(household_id) or
	  summ `var' if e(sample) == 1
	  mat mean_y[`i', 1] =  round(r(mean),.001)
	  mat b[`i', 1] = round(exp(_b[Absent_normal]),.001)
	  mat se[`i', 1] = round(_se[Absent_normal],.001)
	  local p = round(2*(normal(-abs(_b[Absent_normal]/_se[Absent_normal]))),.0001)	
	  mat sig[`i', 1] = `p'
	  mat N[`i', 1] =  e(N)
	  mat lowerci[`i', 1] = round(exp(_b[Absent_normal] - 1.96 * _se[Absent_normal]),.001)
	  mat upperci[`i', 1] = round(exp(_b[Absent_normal] + 1.96 * _se[Absent_normal]),.001)
	}
	

	keep `varlist'
	order `varlist'
	
	keep in 1
	xpose, clear varname
	replace v1 = _n
	
	svmat mean_y
	svmat b
	svmat sig	
	
	* format coef
	gen coef = string(b, "%9.3f")
	replace coef = coef + "*" if sig < 0.1 & sig >= 0.05
	replace coef = coef + "**" if sig < 0.05 & sig >= 0.01
	replace coef = coef + "***" if sig < 0.01

	
	svmat se
	gen sd = "(" + string(se, "%9.3f") + ")"
	
	keep coef* sd* b* v1 _varname mean_y* sig*
	
	svmat lowerci
	svmat upperci
	
	* format 95% confidence interval
	gen ci = "["+ string(lowerci, "%9.3f") + ", " + string(upperci, "%9.3f") + "]"

	svmat N
	order v1 _varname mean_y coef sig ci N 
	
	insobs 1, before(1)
	replace v1 = 0 in 1
	replace _varname = "use" if v1 == 0
	

	replace mean_y = mean_y[2] if v1 == 0
	replace coef = coef[2] if v1 == 0
	replace sd = sd[2] if v1 == 0
	replace sig = sig[2] if v1 == 0
	replace N = N[2] if v1 == 0
	replace b = b[2] if v1 == 0
	replace ci = ci[2] if v1 == 0
	replace lowerci = lowerci[2] if v1 == 0
	replace upperci = upperci[2] if v1 == 0
	
	drop if inlist(_varname, "use_normal")
	
	replace _varname = "Use the facility" if _varname == "use"
	replace _varname = "Seek facility-based care" if _varname == "ever_anycare"
	replace _varname = "Go to public facilities" if _varname == "ever_public"
	replace _varname = "Go to high level public facilities" if _varname == "ever_highlvl_pb"
	replace _varname = "Go to low level public facilities" if _varname == "ever_lowlvl_pb"
	replace _varname = "Go to private facilities" if _varname == "ever_private"
	replace _varname = "Go to retail sectors" if _varname == "ever_drugshop"
	replace _varname = "Receive malaria test" if _varname == "took_malariatest"
	replace _varname = "Take medications" if _varname == "took_medicine"
	replace _varname = "Number of medications" if _varname == "n_drug"
	replace _varname = "Take any antimalarial drugs" if _varname == "took_any_AM"
	replace _varname = "Take antibiotics" if _varname == "took_AB"
	replace _varname = "Length of illness (days)" if _varname == "length_illness"
	replace _varname = "Ln(OOP for medications)" if _varname == "ln_drugcost"
	replace _varname = "Pay OOP for medications" if _varname == "any_drugcost"
	replace _varname = "Illness lasts more than one week" if _varname == "length_illness_1wkormore"		
}
